package com.xiaoshuidi.cloud.module.contract.controller.admin.contract;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.idempotent.core.annotation.Idempotent;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.stay.*;
import com.xiaoshuidi.cloud.module.contract.service.contract.ContractStayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 入住交割")
@RestController
@RequestMapping("/contract/stay")
@Validated
public class ContractStayController {

    @Resource
    private ContractStayService stayService;

    @PostMapping("/create")
    @Operation(summary = "创建入住交割")
//    @PreAuthorize("@ss.hasPermission('contract:stay:create')")
    public CommonResult<Long> createStay(@Valid @RequestBody ContractStayCreateReqVO createReqVO) {
        return success(stayService.createStay(createReqVO));
    }


    @GetMapping("/get")
    @Operation(summary = "获得入住交割")
    @Parameter(name = "id", description = "合同ID", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('contract:stay:query')")
    public CommonResult<ContractStayRespVO> getStay(@RequestParam("id") Long id) {
        return success(stayService.getStay(id));
    }

    @PostMapping("/edit")
    @Operation(summary = "编辑入住交割")
//    @PreAuthorize("@ss.hasPermission('contract:stay:create')")
    public CommonResult<Boolean> editStay(@Valid @RequestBody ContractStayUpdateReqVO updateReqVO) {
        stayService.editStay(updateReqVO);
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获得入住交割分页")
//    @PreAuthorize("@ss.hasPermission('contract:stay:query')")
    public CommonResult<PageResult<ContractStayPageRespVO>> getStayPage(@Valid ContractStayPageReqVO pageVO) {
        return success(stayService.getStayPage(pageVO));
    }

    @GetMapping("/confirm")
    @Operation(summary = "确认交房")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @Idempotent(timeout = 10, message = "重复请求，请稍后重试")
//    @PreAuthorize("@ss.hasPermission('contract:stay:query')")
    public CommonResult<Boolean> confirmDelivery(@RequestParam("id") Long id) {
        stayService.confirmDelivery(id);
        return success(true);
    }

    /**
     * 获得退租交房清单
     */
    @GetMapping("/get-rent-out")
    @Operation(summary = "获得退租交房清单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('contract:stay:query')")
    public CommonResult<ContractRentOutStayRespVO> getRentOutDeliveryStay(@RequestParam("id") Long id) {
        return success(stayService.getRentOutDeliveryStay(id));
    }

}
